package main

import (
	"fmt"
	"local.dev/sqlmaker"
	"local.dev/sqlmaker/config"
)

func main() {
	cfg := &config.Config{
		Database: config.DatabaseConfig{
			Driver:   "mysql",
			Host:     "localhost",
			Port:     3306,
			User:     "root",
			Password: "123456",
			DBName:   "testdb",
			Charset:  "utf8mb4",
		},
	}

	db, err := sqlmaker.Connect(cfg)
	if err != nil {
		panic(err)
	}
	defer db.Close()

	qb := sqlmaker.NewQueryBuilder().
		SelectFrom("users").
		Columns("name", "age").
		Where("age > ?", 20)

	rows, err := qb.Query(db)
	if err != nil {
		panic(err)
	}
	defer rows.Close()

	var users []sqlmaker.User
	if err := sqlmaker.ScanAll(rows, &users); err != nil {
		panic(err)
	}

	if err := sqlmaker.WriteToFile(users, "output.json"); err != nil {
		panic(err)
	}

	fmt.Println("查询结果已写入output.json")
}